﻿using System;
using System.Collections.Generic;
using System.Text;

namespace MyLibrary
{
    /// <summary>
    /// System.Convert的扩展
    /// </summary>
    public static class ConvertPlus
    {
        /// <summary>
        /// 将Byte数组的值转换成字符串,以空格隔开
        /// </summary>
        /// <param name="value">Byte数组</param>
        /// <returns>转换后的数组</returns>
        public static string ToString(byte[] value)
        {
            return ToString(value, " ");
        }

        /// <summary>
        /// 将Byte数组的值转换成字符传,以指定分隔符隔开
        /// </summary>
        /// <param name="value">Byte数组</param>
        /// <param name="spilt">指定分隔符</param>
        /// <returns>转换后的数组</returns>
        public static string ToString(byte[] value,string spilt)
        {
            StringBuilder result = new StringBuilder();
            for (int i = 0; i < value.Length; ++i)
            {
                result.AppendFormat("{0:X2}", value[i]);
                if (i != value.Length - 1)
                    result.Append(spilt);
            }
            return result.ToString();
        }

        /// <summary>
        /// 将一个Byte转换成字符串
        /// </summary>
        /// <param name="value">要转换的Byte值</param>
        /// <returns>转换结果</returns>
        public static string ToString(byte value)
        {
            return String.Format("{0:X2}", value);
        }

        /// <summary>
        /// 将Byte数组的值转换成字符串
        /// </summary>
        /// <param name="value">Byte数组</param>
        /// <param name="addSpace">是否用空格隔开</param>
        /// <param name="addPrefix">是否加0x前缀</param>
        /// <returns>转换结果</returns>
        public static string ToString(byte[] value, bool addSpace, bool addPrefix)
        {
            StringBuilder result = new StringBuilder();
            for (int i = 0; i < value.Length; ++i)
            {
                if (addSpace)
                    result.AppendFormat("0x");
                result.AppendFormat("{0:X2}", value[i]);
                if (addSpace)
                {
                    if (i != value.Length - 1)
                        result.Append(" ");
                }
            }
            return result.ToString();
        }

        public static UInt64 ToUInt64(byte[] value)
        {
            UInt64 result = 0;
            for (int i = 0; i < 8; i++)
            {
                result = result | value[i];
                if (i != 7)
                    result = result << 8;
            }
            return result;
        }

        public static UInt32 ToUInt32(UInt16 high, UInt16 low)
        {
            UInt32 result = high;
            return (result << 16) | low;
        }

        public static UInt32 ToUInt32(byte b1, byte b2, byte b3, byte b4)
        {
            return ToUInt32(ToUInt16(b1, b2), ToUInt16(b3, b4));
        }

        public static UInt32 ToUInt32(byte[] value, int pos)
        {
            return ToUInt32(value[pos], value[pos + 1], value[pos + 2], value[pos + 3]);
        }

        /// <summary>
        /// 将两个Byte转换成UInt16,高位在前,低位在后
        /// </summary>
        /// <param name="hight">高位Byte</param>
        /// <param name="low">低位Byte</param>
        /// <returns>结果</returns>
        public static UInt16 ToUInt16(byte hight, byte low)
        {
            UInt16 result = hight;
            return (UInt16)((result << 8) | low);
        }

        /// <summary>
        /// 将64位无符号正整型转化为数组
        /// </summary>
        /// <param name="value">正整型数</param>
        /// <returns>转换结果</returns>
        public static byte[] ToBytes(UInt64 value)
        {
            byte[] result = new byte[8];
            for (int i = 0; i < 8; i++)
            {
                unchecked
                {
                    result[7 - i] = Convert.ToByte(value & 0x00000000000000FF);
                    if (i != 7)
                        value >>= 8;
                }
            }
            return result;
        }

        /// <summary>
        /// 将32位无符号正整型转化为数组
        /// </summary>
        /// <param name="value">正整型数</param>
        /// <returns>转换结果</returns>
        public static byte[] ToBytes(UInt32 value)
        {
            byte[] result = new byte[4];
            for (int i = 0; i < 4; i++)
            {
                unchecked
                {
                    result[3 - i] = Convert.ToByte(value & 0x000000FF);
                    if (i != 3)
                        value >>= 8;
                }
            }
            return result;
        }

        public static byte[] ToBytes(int value)
        {
            return ToBytes((uint)value);
        }

    }
}
